package com.itheima.kafka.boot.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.kafka.boot.pojo.User;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @Description: 监听类，接收消息
 * @Version: V1.0
 */
@Component
public class HelloListener {

/*

    @KafkaListener(topics = {"kafka-hello"})
    public void receiveMessage(ConsumerRecord<?,?> record) {

        // 接收到消息
//        if (record != null) {
//            System.out.println(record.key());
//            System.out.println(record.value());
//        }
        // Optional JDK8 新特性， 专门判断对象非空的标准类
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.of(record);
        // 判断非空
        if (optional.isPresent()) {
            record = optional.get();
            System.out.println(record.key());
            System.out.println(record.value());
        }

    }
*/

/*

    @KafkaListener(topics = {"kafka-hello"})
    public void receiveMessage(String message) {

        if (!StringUtils.isEmpty(message)) {
            System.out.println(message);
        }
     }
*/



    @KafkaListener(topics = {"kafka-hello"})
    public void receiveMessage(String message) {

        if (!StringUtils.isEmpty(message)) {
            System.out.println(message);

            User user = JSON.parseObject(message, User.class);
            System.out.println(user);
        }
    }

}
